package com.zxst.service.impl;

import com.zxst.entity.Goods;
import com.zxst.mapper.GoodsMapper;
import com.zxst.mapper.MemberMapper;
import com.zxst.mapper.OrdersMapper;
import com.zxst.service.ReportService;
import com.zxst.util.DateUtils;
import com.zxst.util.JsonResult;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.*;

@Service
public class ReportServiceImpl implements ReportService {

    @Resource
    private MemberMapper memberMapper;

    @Resource
    private GoodsMapper goodsMapper;

    @Resource
    private OrdersMapper ordersMapper;

    //会员折线图
    @Override
    public JsonResult getMemeberCount() {
        Map<String,Object> map = new HashMap<>();
        List<String> nameList = new ArrayList<>(); // "2024-07,2024-08"
        List<Integer> ctList = new ArrayList<>(); // "3,4,5,6,7"
        //转换日期的工具
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
        Calendar calendar = Calendar.getInstance(); // 2025--06-25
        calendar.add(Calendar.MONTH, -12); // 2024-06-25
        for (int i = 0; i < 12; i++) {
            calendar.add(Calendar.MONTH, 1);
            Date time = calendar.getTime();
            String format = sdf.format(time);// "2024-07...."
            //将月分的数据添加到容器中
            nameList.add(format);
            //获取每个月份的人数
            String  start = format + "-01";
            String end = format + "-30";
            int ctByDate = memberMapper.getCtByDate(start, end);
            ctList.add(ctByDate);
        }
        map.put("months", nameList);
        map.put("memeberCount", ctList);
        return new JsonResult(200,null,map);
    }

    //柱形图 商品和销售金额
    @Override
    public JsonResult getGoodsAndMoney() {
        Map<String,Object> map = new HashMap<>();
        List<String> nameList = new ArrayList<>(); // "问界M9,小米SU7,MODELY"
        List<Long> ctList = new ArrayList<>(); // "23,34,6,6,7,8,8"
        //获取商品信息
        List<Goods> goodsList = goodsMapper.selectList(null);
        //循环
        goodsList.forEach(goods -> {
            String gname = goods.getGname();
            nameList.add(gname);
            Long countByPid = ordersMapper.getCountByPid(goods.getGid());
            ctList.add(countByPid);
        });
        map.put("goodsName", nameList);
        map.put("totalMoney", ctList);
        return new JsonResult(200,null,map);
    }

    //商品销售数量饼状图
    @Override
    public JsonResult getGoodsNumInfo() {
        Map<String,Object> map = new HashMap<>();
        List<Integer> ct1 = new ArrayList<>();
        List<Integer> ct2 = new ArrayList<>();
        List<Integer> ct3 = new ArrayList<>();
        List<String> ct4 = new ArrayList<>();
        List<String> gname = new ArrayList<>();

        List<Goods> goodsList = goodsMapper.selectList(null);
        int index = 0;
        int index2 = 1;
        int index3 = 2;
        for (Goods goods : goodsList) {
            gname.add(goods.getGname());
            ct1.add(index++);
            ct2.add(index2++);
            ct3.add(++index3);
        }
        ct4.add("一月销量");
        ct4.add("一月销量");
        ct4.add("一月销量");

        map.put("goodsName",gname);
        map.put("ct1",ct1);
        map.put("ct2",ct2);
        map.put("ct3",ct3);
        map.put("ct4",ct4);

        return new JsonResult(200,null,map);
    }

    @Override
    public JsonResult getBusinessReportData() {
        Map<String,Object> map = new HashMap<>();
        //获取今日的日期信息
        try {
            Date today = DateUtils.getToday();
            String todayStr = DateUtils.parseDate2String(today);
            map.put("reportDate",todayStr);

            Integer ct = memberMapper.getTodayMember(todayStr);
            map.put("todayNewMember",ct);

            Integer total = memberMapper.getTotalMember();
            map.put("totalMember",total);

        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return new JsonResult(200,null,map);
    }
}
